package zy.blue7.gui.netty.learn.simple;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;

/**
 * @author blue7
 * @version 1.0
 * @date 2022/2/25 15:35
 */
public class HelloClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        System.out.println("client: "+ctx);

        ctx.writeAndFlush(Unpooled.copiedBuffer("hello server : ----------", CharsetUtil.UTF_8));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        ByteBuf buf= (ByteBuf) msg;

        System.out.println("服务器回复的消息是："+buf.toString(CharsetUtil.UTF_8));

        System.out.println("服务器地址："+ctx.channel().remoteAddress());

        /**
         * todo 必须调用下面方法， 整个 handler 处理器才会继续向下走，数据才会继续被处理
         */
        ctx.fireChannelRead(msg);
    }
}
